package com.bw.ivideoproject.push

import android.hardware.Camera
import android.media.MediaRecorder
import android.os.Environment
import android.view.SurfaceHolder
import com.bw.ivideoproject.R
import com.bw.ivideoproject.databinding.ActivityCameraBinding
import com.bw.lib_mvvm.BaseActivity
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream

class CameraActivity : BaseActivity<ActivityCameraBinding>() ,SurfaceHolder.Callback {

    private var mCamera: Camera? = null
    private var bufferedOutputStream: BufferedOutputStream? = null
    private var isStart = false
    private var mediaRecorder: MediaRecorder? = null

    override fun initView() {
        binding.surfaceView.holder.addCallback(this)
        mCamera = Camera.open(0)
        mCamera?.setDisplayOrientation(90)

        binding.takePhoto.setOnClickListener {
            mCamera?.takePicture(null,null,object : Camera.PictureCallback{
                override fun onPictureTaken(data: ByteArray?, camera: Camera?) {
                    val fileName = "${System.currentTimeMillis()}.jpg"
                    val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).absolutePath + File.separator + fileName)
                    Thread{
                        bufferedOutputStream = BufferedOutputStream(FileOutputStream(file))
                        bufferedOutputStream?.write(data)
                        bufferedOutputStream?.close()
                    }.start()
                    showMsg("拍照完成")
                }
            })
        }

        binding.takeVideo.setOnClickListener {
            if(!isStart){
                val fileName = "${System.currentTimeMillis()}.mp4"
                val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).absolutePath + File.separator + fileName)
                mediaRecorder = MediaRecorder()
                mCamera?.unlock()
                mediaRecorder?.setCamera(mCamera)
                mediaRecorder?.setAudioSource(MediaRecorder.AudioSource.MIC)
                mediaRecorder?.setVideoSource(MediaRecorder.VideoSource.CAMERA)
                mediaRecorder?.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
                mediaRecorder?.setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
                mediaRecorder?.setVideoEncoder(MediaRecorder.VideoEncoder.H264)
                mediaRecorder?.setVideoSize(1280, 720)
                mediaRecorder?.setOutputFile(file.absolutePath)
                mediaRecorder?.setPreviewDisplay(binding.surfaceView.holder.surface)
                mediaRecorder?.prepare()
                mediaRecorder?.start()
                isStart = true
                binding.takeVideo.text = "停止录制"
                showMsg("录制开始")
            }else{
                mediaRecorder?.stop()
                mediaRecorder?.reset()
                mediaRecorder?.release()
                mCamera?.lock()
                binding.takeVideo.text = "录制视频"
                mediaRecorder = null
                isStart = false
                showMsg("录制完成")
            }
        }
    }

    override fun initData() {

    }

    override fun bindLayout(): Int {
        return R.layout.activity_camera
    }

    override fun surfaceCreated(holder: SurfaceHolder) {
        mCamera?.setPreviewDisplay(holder)
        mCamera?.startPreview()
    }

    override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
    }


    override fun surfaceDestroyed(holder: SurfaceHolder) {
        mCamera?.stopPreview()
        mCamera?.setPreviewCallback(null)
        mCamera?.release()
        mCamera = null
    }

}